package com.xy.biz.ts.domain.converter;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.xy.biz.ts.domain.bo.ExaminationQuestionOptionBO;
import com.xy.biz.ts.domain.req.*;
import com.xy.biz.ts.domain.resp.DifficultyResponse;
import com.xy.biz.ts.domain.resp.GetSingleExaminationQuestionResponse;
import com.xy.biz.ts.domain.resp.PreviewPaperExaminationQuestionResponse;
import com.xy.tool.util.BeanUtilsEx;
import com.xy.biz.ts.domain.bo.ExaminationQuestionBO;
import com.xy.biz.ts.domain.po.ExaminationQuestionPO;
import com.xy.biz.ts.domain.query.ExaminationQuestionQuery;
import com.xy.biz.ts.domain.resp.GetExaminationQuestionResponse;

/**
 *
 *
 * @author wcs
 * @date 2021-3-8
 */
public class ExaminationQuestionConverter {

    public static ExaminationQuestionPO toPO(ExaminationQuestionBO source) {
        return BeanUtilsEx.copyProperties(source, () -> new ExaminationQuestionPO(), true);
    }

    public static List<ExaminationQuestionPO> toPO(Collection<ExaminationQuestionBO> source) {
        return BeanUtilsEx.copyPropertiesForNewList(source, () -> new ExaminationQuestionPO(), true);
    }

    public static ExaminationQuestionBO fromPO(ExaminationQuestionPO source) {
        return BeanUtilsEx.copyProperties(source, () -> new ExaminationQuestionBO());
    }

    public static List<ExaminationQuestionBO> fromPO(Collection<ExaminationQuestionPO> source) {
        return BeanUtilsEx.copyPropertiesForNewList(source, () -> new ExaminationQuestionBO());
    }

    public static ExaminationQuestionBO toBO(SaveExaminationQuestionRequest source) {
        return BeanUtilsEx.copyProperties(source, () -> new ExaminationQuestionBO(),
                (src, tar) ->
                        tar.setOptions(BeanUtilsEx.copyPropertiesForNewList(src.getOptions(), () -> new ExaminationQuestionOptionBO()))
        );
    }

    public static ExaminationQuestionBO toBO(UpdateExaminationQuestionRequest source) {
        return BeanUtilsEx.copyProperties(source, () -> new ExaminationQuestionBO(),
                (src, tar) ->
                        tar.setOptions(BeanUtilsEx.copyPropertiesForNewList(src.getOptions(), () -> new ExaminationQuestionOptionBO()))
        );
    }

//    public static ExaminationQuestionBO toBO(SaveExaminationQuestionRequest source) {
//        ExaminationQuestionBO target = new ExaminationQuestionBO();
//        BeanUtilsEx.copyProperties(source, target);
//        target.setOptionList(BeanUtilsEx.copyPropertiesForNewList(source.getOptionList(),
//                () -> new ExaminationQuestionOptionBO()));
//        return target;
//    }

//	public static List<ExaminationQuestionBO> toBO(Collection<SaveExaminationQuestionRequest> source) {
//		return BeanUtilsEx.copyPropertiesForNewList(source, () -> new ExaminationQuestionBO(), true);
//	}

    public static List<ExaminationQuestionBO> toBO(Collection<SaveExaminationQuestionRequest> source) {
        return BeanUtilsEx.copyPropertiesForNewList(source, () -> new ExaminationQuestionBO(),
                (src, tar) -> {
                    BeanUtilsEx.copyProperties(src, tar);
                    tar.setOptions(BeanUtilsEx.copyPropertiesForNewList(src.getOptions(), () -> new ExaminationQuestionOptionBO()));
                });
    }

    public static GetSingleExaminationQuestionResponse toResponse(ExaminationQuestionBO source) {
        GetSingleExaminationQuestionResponse getExaminationQuestionResponse = BeanUtilsEx.copyProperties(source, () -> new GetSingleExaminationQuestionResponse());
        //1-容易、2-常规、3-较难、4-很难
        switch (getExaminationQuestionResponse.getDifficulty()) {
            case 1:
                getExaminationQuestionResponse.setDifficultyResponse(new DifficultyResponse(1, "容易"));
                break;
            case 2:
                getExaminationQuestionResponse.setDifficultyResponse(new DifficultyResponse(2, "常规"));
                break;
            case 3:
                getExaminationQuestionResponse.setDifficultyResponse(new DifficultyResponse(3, "较难"));
                break;
            case 4:
                getExaminationQuestionResponse.setDifficultyResponse(new DifficultyResponse(4, "很难"));
                break;
            default:
        }
        return getExaminationQuestionResponse;
    }

    public static List<GetExaminationQuestionResponse> toResponse(Collection<ExaminationQuestionBO> source) {
        Map<Long, List<ExaminationQuestionOptionBO>> opMap = source.stream().collect(HashMap::new, (map, item) -> map.put(item.getExaminationQuestionId(), item.getOptions()), HashMap::putAll);
        List<GetExaminationQuestionResponse> getExaminationQuestionResponses = BeanUtilsEx.copyPropertiesForNewList(source, () -> new GetExaminationQuestionResponse());
        for (GetExaminationQuestionResponse resp : getExaminationQuestionResponses) {
            resp.setOptions(ExaminationQuestionOptionConverter.toResponse(opMap.get(resp.getExaminationQuestionId())));
        }
        return getExaminationQuestionResponses;
    }

    public static List<PreviewPaperExaminationQuestionResponse> toPreviewPaperResponse(Collection<ExaminationQuestionBO> source) {
        Map<Long, List<ExaminationQuestionOptionBO>> opMap = source.stream().collect(HashMap::new, (map, item) -> map.put(item.getExaminationQuestionId(), item.getOptions()), HashMap::putAll);
        List<PreviewPaperExaminationQuestionResponse> getExaminationQuestionResponses = BeanUtilsEx.copyPropertiesForNewList(source, () -> new PreviewPaperExaminationQuestionResponse());
        getExaminationQuestionResponses.forEach(
                resp -> resp.setOptions(ExaminationQuestionOptionConverter.toResponse(opMap.get(resp.getExaminationQuestionId())))
        );
        return getExaminationQuestionResponses;
    }

    public static ExaminationQuestionQuery toQuery(ListExaminationQuestionRequest source) {
        if (null == source) {
            return null;
        }
        ExaminationQuestionQuery target = new ExaminationQuestionQuery(source.getPageIndex(),
                source.getPageSize());
        BeanUtilsEx.copyProperties(source, target);
        return target;
    }

    public static ExaminationQuestionQuery toQuery(CountExaminationPaperRuleRequest source) {
        return BeanUtilsEx.copyProperties(source, () -> new ExaminationQuestionQuery(0, Integer.MAX_VALUE), (src, tar) -> {
            tar.setCategory(src.getQuestionCategory());
            tar.setType(src.getQuestionType());
            tar.setDifficulty(src.getDifficulty());
            tar.setScore(src.getScore());
        });
    }

}
